package com.liming.StudentSystem;

import java.util.ArrayList;
import java.util.Scanner;


public class studentSystem {
    public static void main(String[] args) {
        ArrayList<Students> list = new ArrayList<>();
        while (true) {
            System.out.println("----------欢迎使用学生信息管理系统----------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出系统");
            System.out.println("----------欢迎使用学生信息管理系统----------");
            System.out.println("请输入你的选择：");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            switch (choice) {
                case 1->addStudent(list);
                case 2-> deleteStudent(list);
                case 3-> updateStudent(list);
                case 4-> queryStudent(list);
                case 5 -> {
                    System.out.println("退出系统");
                    System.exit(0);
                }
                default->
                    System.out.println("输入有误");
            }
        }

    }
    public static void addStudent(ArrayList<Students> list) {
        System.out.println("添加学生");
        Students student = new Students();
        Scanner scanner = new Scanner(System.in);
        //键盘录入学生信息
        while (true) {
            System.out.println("请输入学生的id：");
            String id = scanner.next();
            //判断id是否存在
            boolean flag = containsId(list, id);
            if (flag) {
                System.out.println("id已存在");
            }else {
                //id不存在,可以添加
                student.setId(id);
                break;

            }
        }
        System.out.println("请输入学生的姓名：");
        String name = scanner.next();
        student.setName(name);
        System.out.println("请输入学生的年龄：");
        int age = scanner.nextInt();
        student.setAge(age);
        System.out.println("请输入学生的家庭住址：");
        String address = scanner.next();
        student.setAddress(address);

        //将学生对象添加到集合中
        list.add(student);
        System.out.println("学生信息添加成功");
    }
    public static void deleteStudent(ArrayList<Students> list){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的学生id：");
        String id = scanner.next();
        //判断id是否存在
        int index = getIndex(list,id);
        //大于等于0，id存在，删除
        //如果-1，id不存在，回到主菜单
        if (index >= 0) {
            list.remove(index);
            System.out.println("id为："+id+"学生信息删除成功");
        }
        else {
            System.out.println("id不存在,删除失败");
        }
    }
    public static void updateStudent(ArrayList<Students> list){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的学生id：");
        String id = scanner.next();
        //判断id是否存在
        int index = getIndex(list,id);
        //大于等于0，id存在，修改
        //如果-1，id不存在，回到主菜单
        if (index >= 0) {
            Students student = list.get(index);
            System.out.println("请输入学生的姓名：");
            String newName = scanner.next();
            student.setName(newName);
            System.out.println("请输入学生的年龄：");
            int newAge = scanner.nextInt();
            student.setAge(newAge);
            System.out.println("请输入学生的家庭住址：");
            String newAddress = scanner.next();
            student.setAddress(newAddress);
            System.out.println("学生信息修改成功");
        }
        else {
            System.out.println("id不存在,修改失败");
        }
    }
    public static void queryStudent(ArrayList<Students> list){
        if (list.size() == 0) {
            System.out.println("当前无学生信息，请先添加信息后再查询");
            //结束方法
            return;
        }
        System.out.println("id\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Students student = list.get(i);
            System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getAddress());
        }
    }
    //判断id是否存在
    public static boolean containsId(ArrayList<Students> list,String id){
//        for (int i = 0; i < list.size(); i++) {
//            Students student = list.get(i);
//            if (student.getId().equals(id)) {
//                return true;
//            }
//        }
//        return false;
        return getIndex(list,id) >= 0;
    }
    //获取id在集合中的索引
    public static int getIndex(ArrayList<Students> list,String id){
        for (int i = 0; i < list.size(); i++) {
            Students student = list.get(i);
            if (student.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
